/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Classes;
import com.yuhanginfo.modules.sys.entity.Materiel;
import com.yuhanginfo.modules.sys.entity.Parts;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.entity.Vender;
import com.yuhanginfo.modules.sys.service.ClassesService;
import com.yuhanginfo.modules.sys.service.MaterielService;
import com.yuhanginfo.modules.sys.service.PartsService;
import com.yuhanginfo.modules.sys.service.VenderService;
import com.yuhanginfo.modules.sys.utils.CodeUtil;
import com.yuhanginfo.modules.sys.utils.PartsUtil;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 物料管理Controller
 * @author yangs
 * @version 2017-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/materiel")
public class MaterielController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(MaterielController.class);
	
	@Autowired
	private MaterielService materielService;
	@Autowired
	private VenderService venderService;
	@Autowired
	private ClassesService classesService ;
	@Autowired
	private PartsService partsService;
	
	private static final String REDIRECT = "redirect:"+Global.getAdminPath()+"/sys/materiel/materList?repage";
	
	private static final String VENDER = "vender";
	
	private static final String PARTS = "parts";
	
	private static final String CLASSES = "classes";
	
	private static final String MATERIEL = "materiel";
	
	private static final String RESULTCODE = "resultCode";
	@ModelAttribute
	public Materiel get(@RequestParam(required=false) String id) {
		Materiel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materielService.get(id);
		}
		if (entity == null){
			entity = new Materiel();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = {"list", ""})
	public String list(Materiel materiel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Materiel> page = materielService.findPage(new Page<Materiel>(request, response), materiel); 
		model.addAttribute("page", page);
		return "modules/sys/materielList";
	}
	
	
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "form")
	public String form(Materiel materiel, Model model) {
		List<Classes>  classes=classesService.selectFirstClass();
		List<Parts>  parts=PartsUtil.partList();
		List<Vender> vender=venderService.selectVenderList();
		model.addAttribute(VENDER, vender);
		model.addAttribute(PARTS, parts);
		model.addAttribute(CLASSES, classes);
		model.addAttribute(MATERIEL, materiel);
		model.addAttribute("ps", 1);
		return "modules/sys/materielList/materielForm";
	}
	
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "searchParts")
	@ResponseBody
	public Map<String,Object> searchParts(Parts parts) {
		Map<String,Object> resultMap = new HashMap<>();
		try{
			if(null == parts.getCode() || "".equals(parts.getCode())){
				resultMap.put(RESULTCODE, 0);
				resultMap.put("resultMessage", "请输入配件号");
				return resultMap;
			}
			resultMap.put("list",partsService.findList(parts));
			resultMap.put(RESULTCODE, 1);
		}catch(Exception ex){
			logger.info("searchParts",ex);
			resultMap.put(RESULTCODE, 0);
		}
		return resultMap;
	}

	@RequiresPermissions("sys:materiel:edit")
	@RequestMapping(value = "save")
	public String save(Materiel materiel, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request,String ps) {
		Classes class3=classesService.get(materiel.getClassessThirdId().toString()) ;
		//ps为1代表新增
		if("1".equals(ps)){
			String materielCode = CodeUtil.getMaterielCode(class3.getCode());
			materiel.setCode(materielCode);
			materiel.setCreateDate(new Date());
		}
		User us = UserUtils.getUser();
		materiel.setSysUserId(us.getId());
		materiel.setUpdateDate(new Date());
		materiel.setAuditingStatus(0);
		materiel.setCreateBy(us);
		if (!beanValidator(model, materiel)){
			return form(materiel, model);
		}
		materielService.save(materiel);
		addMessage(redirectAttributes, "保存物料管理成功");
		return REDIRECT;
	}
	
	@RequiresPermissions("sys:materiel:edit")
	@RequestMapping(value = "delete")
	public String delete(Materiel materiel, RedirectAttributes redirectAttributes) {
		materielService.delete(materiel);
		addMessage(redirectAttributes, "删除物料管理成功");
		return REDIRECT;
	}
   
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value ="searchMaterList")
	public String searchMaterList(Materiel materiel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Materiel> page = materielService.materList(new Page<Materiel>(request, response), materiel); 
		List<Vender> vender=venderService.selectVenderList();
		model.addAttribute(VENDER, vender);
		model.addAttribute("page", page);
		return "modules/sys/materielList/materielList";
	}
	
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value ="materList")
	public String materList(Materiel materiel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Materiel> page = new Page<>(request, response); 
		List<Vender> vender=venderService.selectVenderList();
		model.addAttribute(VENDER, vender);
		model.addAttribute("page", page);
		return "modules/sys/materielList/materielList";
	}
	
	@ResponseBody
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "twiceClassData")
	public List<Classes> twiceClassData(String useId) {
		return classesService.twiceClassList(useId);
	}
	
	@ResponseBody
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "thirdClassData")
	public List<Classes> thirdClassData(String useId) {
		return classesService.twiceClassList(useId);
	}
	
	@ResponseBody
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "partsData")
	public Parts partsData(String code) {
		return partsService.selectByCode(code);
	}
	
	//编辑物料显示
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "viewForm")
	public String viewForm( Model model,String useId) {
		Materiel materiel=materielService.selectById(useId);
		List<Classes>  classes=classesService.selectFirstClass();
		List<Parts>  parts=PartsUtil.partList();
		List<Vender> vender=venderService.selectVenderList();
		model.addAttribute("twices",materiel.getClassessTwiceId() );
		model.addAttribute("thirds",materiel.getClassessThirdId() );
		model.addAttribute(VENDER, vender);
		model.addAttribute(PARTS, parts);
		model.addAttribute(CLASSES, classes);
		model.addAttribute(MATERIEL, materiel);
		model.addAttribute("ps", 2);
		return "modules/sys/materielList/materielForm";
	}
	
	//物料审核列表
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value ="auditingMaterList")
	public String auditingMaterList(Materiel materiel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Materiel> page = materielService.auditingMaterList(new Page<Materiel>(request, response), materiel); 
		model.addAttribute("page", page);
		return "modules/sys/materielList/auditingList";
	}
	
	//编辑物料显示
	@RequiresPermissions("sys:materiel:edit")
	@RequestMapping(value = "updateAuditing")
	public String updateAuditing( Model model,String useId,Materiel materiel,String auditingStatus,String auditingRemark ) {
		materiel.setAuditingStatus(Integer.parseInt(auditingStatus));
		materiel.setAuditingRemark(auditingRemark);
		materiel.setUpdateDate(new Date());
		materiel.setId(useId);
		materielService.updateAuditingStatus(materiel);
		return "redirect:"+Global.getAdminPath()+"/sys/materiel/auditingMaterList?repage";
	}
	
	  //编辑物料显示
	@RequiresPermissions("sys:materiel:view")
	@RequestMapping(value = "viewMateriel")
	public String viewMateriel( Model model,String useId) {
		Materiel materiel=materielService.selectById(useId);
		List<Classes>  classes=classesService.selectFirstClass();
		List<Parts>  parts=PartsUtil.partList();
		List<Vender> vender=venderService.selectVenderList();
		model.addAttribute("twices",materiel.getClassessTwiceId() );
		model.addAttribute("thirds",materiel.getClassessThirdId() );
		model.addAttribute(VENDER, vender);
		model.addAttribute(PARTS, parts);
		model.addAttribute(CLASSES, classes);
		model.addAttribute("materiel", materiel);
		model.addAttribute("ps", 2);
		return "modules/sys/materielList/viewForm";
	}
	
	@ResponseBody
	@RequiresPermissions("user")
	@RequestMapping(value = "getMaterielList")
	public Map<String,Object> getMaterielList(Materiel materiel) {
		Map<String,Object> map = new HashMap<>();
		map.put("list", materielService.findList(materiel)); 
		map.put(RESULTCODE, "1");
		return map;
	}
}